package com.sskj.spk.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.just.agentweb.AgentWeb;
import com.sskj.spk.R;
import com.sskj.spk.presenter.BasePresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import butterknife.BindView;

@Route(path = RConfig.APP_BANNER_WEBVIEW)//行情Banner详情
public class BannerWebActivity extends BaseActivity<BasePresenter> {
    @BindView(R.id.banner_web)
    WebView bannerWeb;
    @BindView(R.id.llWeb)
    LinearLayout llWeb;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivBack)
    ImageView ivBack;

    private AgentWeb mAgentWeb;


    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_banner_web;
    }

    @Override
    public BasePresenter getPresenter() {
        return new BasePresenter();
    }

    public static void start(Context context, String url) {
        Intent intent = new Intent(context, BannerWebActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url)) {
            if (url.contains("http")) {
                initView(url);
            } else {
                initView("http://" + url);
            }
        }
    }
    public void initView(String url) {

        ((ViewGroup) (bannerWeb.getParent())).removeView(bannerWeb);
        bannerWeb.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        // 设置加载进来的页面自适应手机屏幕
        bannerWeb.getSettings().setUseWideViewPort(true);
        bannerWeb.getSettings().setLoadWithOverviewMode(true);
//        bannerWeb.loadUrl(url);

        mAgentWeb = AgentWeb.with(this)//这里需要把 Activity 、 和 Fragment  同时传入
                .setAgentWebParent(llWeb, new LinearLayout.LayoutParams(-1, -1))
//                .useDefaultIndicator(Color.TRANSPARENT)// 使用默认进度条
                .useDefaultIndicator()// 使用默认进度条
                .setWebView(bannerWeb)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //注意这里开启 strict 模式 ， 设备低于 4.2 情况下回把注入的 Js 全部清空掉 ， 这里推荐使用 onJsPrompt 通信
                .createAgentWeb()
                .ready()
                .go(url);
        mAgentWeb.getAgentWebSettings().getWebSettings().setUseWideViewPort(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setLoadWithOverviewMode(true);
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.i("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView webView, String url) {
//
//            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) webView.getLayoutParams();
//            params.width = ScreenUtils.getScreenWidth(WebViewActivity.this);
//            params.height = ScreenUtils.getScreenHeight(WebViewActivity.this) - webView.getTop();
//            webView.setLayoutParams(params);
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

        }
    };

    /**
     * 返回键返回上一网页
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //多加一个判断 如果当前url和intent传进来的url是相等的 取反->走closeActivity()
//        if (keyCode == KeyEvent.KEYCODE_BACK && bannerWeb.canGoBack()) {
//            bannerWeb.goBack();
//            return true;
//        } else {
//            finish();
//        }
        return super.onKeyDown(keyCode, event);
    }
}
